﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using LeadThing.AbpZeroTemplate.CommandDispatches.Authorization;
using LeadThing.AbpZeroTemplate.CommandDispatches.Dtos;
using LeadThing.AbpZeroTemplate.Dto;

namespace LeadThing.AbpZeroTemplate.CommandDispatches
{
    /// <summary>
    /// 指挥调度服务实现
    /// </summary>
    [AbpAuthorize(CommandDispatchAppPermissions.CommandDispatch)]


    public class CommandDispatchAppService : AbpZeroTemplateAppServiceBase, ICommandDispatchAppService
    {
        private readonly IRepository<CommandDispatch, long> _commandDispatchRepository;
        private readonly ICommandDispatchListExcelExporter _commandDispatchListExcelExporter;


        private readonly CommandDispatchManage _commandDispatchManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public CommandDispatchAppService(IRepository<CommandDispatch, long> commandDispatchRepository,
CommandDispatchManage commandDispatchManage
      , ICommandDispatchListExcelExporter commandDispatchListExcelExporter
  )
        {
            _commandDispatchRepository = commandDispatchRepository;
            _commandDispatchManage = commandDispatchManage;
            _commandDispatchListExcelExporter = commandDispatchListExcelExporter;
        }


        #region 实体的自定义扩展方法
        private IQueryable<CommandDispatch> _commandDispatchRepositoryAsNoTrack => _commandDispatchRepository.GetAll().AsNoTracking();


        #endregion


        #region 指挥调度管理

        /// <summary>
        /// 根据查询条件获取指挥调度分页列表
        /// </summary>
        public async Task<PagedResultDto<CommandDispatchListDto>> GetPagedCommandDispatchsAsync(GetCommandDispatchInput input)
        {

            var query = _commandDispatchRepositoryAsNoTrack;
            //TODO:根据传入的参数添加过滤条件
            if (!string.IsNullOrWhiteSpace(input.Filter))
            {
                query = query.Where(p => p.Title.Contains(input.Filter));
            }

            var commandDispatchCount = await query.CountAsync();

            var commandDispatchs = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var commandDispatchListDtos = commandDispatchs.MapTo<List<CommandDispatchListDto>>();
            return new PagedResultDto<CommandDispatchListDto>(
            commandDispatchCount,
            commandDispatchListDtos
            );
        }

        /// <summary>
        /// 通过Id获取指挥调度信息进行编辑或修改 
        /// </summary>
        public async Task<GetCommandDispatchForEditOutput> GetCommandDispatchForEditAsync(NullableIdDto<long> input)
        {
            var output = new GetCommandDispatchForEditOutput();

            CommandDispatchEditDto commandDispatchEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _commandDispatchRepository.GetAsync(input.Id.Value);
                commandDispatchEditDto = entity.MapTo<CommandDispatchEditDto>();
            }
            else
            {
                commandDispatchEditDto = new CommandDispatchEditDto();
            }

            output.CommandDispatch = commandDispatchEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取指挥调度ListDto信息
        /// </summary>
        public async Task<CommandDispatchListDto> GetCommandDispatchByIdAsync(EntityDto<long> input)
        {
            var entity = await _commandDispatchRepository.GetAsync(input.Id);

            return entity.MapTo<CommandDispatchListDto>();
        }







        /// <summary>
        /// 新增或更改指挥调度
        /// </summary>
        public async Task CreateOrUpdateCommandDispatchAsync(CreateOrUpdateCommandDispatchInput input)
        {
            if (input.CommandDispatchEditDto.Id.HasValue)
            {
                await UpdateCommandDispatchAsync(input.CommandDispatchEditDto);
            }
            else
            {
                await CreateCommandDispatchAsync(input.CommandDispatchEditDto);
            }
        }

        /// <summary>
        /// 新增指挥调度
        /// </summary>
        [AbpAuthorize(CommandDispatchAppPermissions.CommandDispatch_CreateCommandDispatch)]
        public virtual async Task<CommandDispatchEditDto> CreateCommandDispatchAsync(CommandDispatchEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<CommandDispatch>();

            entity = await _commandDispatchRepository.InsertAsync(entity);
            return entity.MapTo<CommandDispatchEditDto>();
        }

        /// <summary>
        /// 编辑指挥调度
        /// </summary>
        [AbpAuthorize(CommandDispatchAppPermissions.CommandDispatch_EditCommandDispatch)]
        public virtual async Task UpdateCommandDispatchAsync(CommandDispatchEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _commandDispatchRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _commandDispatchRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除指挥调度
        /// </summary>
        [AbpAuthorize(CommandDispatchAppPermissions.CommandDispatch_DeleteCommandDispatch)]
        public async Task DeleteCommandDispatchAsync(EntityDto<long> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _commandDispatchRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除指挥调度
        /// </summary>
        [AbpAuthorize(CommandDispatchAppPermissions.CommandDispatch_DeleteCommandDispatch)]
        public async Task BatchDeleteCommandDispatchAsync(List<long> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _commandDispatchRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        #endregion

        #region 指挥调度的Excel导出功能


        public async Task<FileDto> GetCommandDispatchToExcel()
        {
            var entities = await _commandDispatchRepository.GetAll().ToListAsync();

            var dtos = entities.MapTo<List<CommandDispatchListDto>>();

            var fileDto = _commandDispatchListExcelExporter.ExportCommandDispatchToFile(dtos);



            return fileDto;
        }


        #endregion

    }
}
